package com.kx.center.service.impl;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kx.center.common.Constant;
import com.kx.center.common.CustomHashMap;
import com.kx.center.entity.Account;
import com.kx.center.entity.Advertisement;
import com.kx.center.entity.Food;
import com.kx.center.entity.FoodCategory;
import com.kx.center.entity.FoodType;
import com.kx.center.entity.query.QueryFoodCategory;
import com.kx.center.entity.query.QueryResult;
import com.kx.center.mappers.FoodMapper;
import com.kx.center.service.FoodService;
import com.kx.center.util.FileUploadUtils;
import com.kx.center.util.ImageUtil;
import com.kx.center.util.JSONParser;

@Service
public class FoodServiceImpl implements FoodService {

	@Autowired
	FoodMapper foodMapper;

	@Override
	public boolean insertFoodCategory(FoodCategory foodCategory) {
		int status = foodMapper.insertFoodCategory(foodCategory);
		if (status > 0) {
			return true;
		}
		return false;
	}

	@Override
	public QueryResult queryFoodCategory(QueryFoodCategory queryFoodCategory) {
		QueryResult result = new QueryResult();
		result.setPage(queryFoodCategory.getPage());
		result.setPageSize(queryFoodCategory.getPageSize());
		result.setTotalRecord(foodMapper
				.findFoodCategoryCount(queryFoodCategory));
		result.setData(foodMapper.findFoodCategoryList(queryFoodCategory));
		return result;
	}

	@Override
	public void updateFoodCategoryById(FoodCategory foodCategory) {
		foodMapper.updateFoodCategoryById(foodCategory);
	}
	
	@Override
	public void deleteFoodCategoryById(Account account, Integer id) {
		FoodCategory foodCategory = new FoodCategory();
		foodCategory.setId(id);
		foodCategory.setOperatorId(account.getOperatorId());
		foodCategory.setStatus(2);
		foodMapper.deleteFoodCategoryById(foodCategory);
	}

	@Override
	public FoodCategory findFoodCategoryByName(String name, Integer companyId) {
		return foodMapper.findFoodCategoryByName(name, companyId);
	}

	@Override
	public String findAllFood(Integer companyId) {
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			
			//查询所有商品
			List<FoodCategory> foodList = foodMapper.findAllFood(companyId);
			map.put("data", foodList);
			
			//查询广告信息
			try {
				List<Advertisement> adList = foodMapper.findAdvertisement();
				map.put("adInfo", adList);
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			
			map.put("status", "success");
			map.put("code", "0");
			map.put("msg", "");
			return JSONParser.stringify(map);
		} catch (JsonProcessingException e) {
			HashMap<String, Object> maps = new HashMap<String, Object>();
			maps.put("status", "error");
			maps.put("code", "1001");
			maps.put("msg", "发生错误");
			try {
				return JSONParser.stringify(maps);
			} catch (JsonProcessingException e1) {
				e1.printStackTrace();
			}
		}
		return null;
	}

	@Override
	public boolean insertFoodType(FoodType foodType) {
		int status = foodMapper.insertFoodType(foodType);
		if (status > 0) {
			return true;
		}
		return false;
	}

	@Override
	public QueryResult queryFoodType(QueryFoodCategory queryFoodCategory) {
		QueryResult result = new QueryResult();
		result.setPage(queryFoodCategory.getPage());
		result.setPageSize(queryFoodCategory.getPageSize());
		result.setTotalRecord(foodMapper.findFoodTypeCount(queryFoodCategory));
		result.setData(foodMapper.findFoodTypeList(queryFoodCategory));
		return result;
	}

	@Override
	public FoodType findFoodTypeByName(String name, Integer companyId) {
		return foodMapper.findFoodTypeByName(name, companyId);
	}

	@Override
	public void updateFoodTypeById(FoodType foodType) {
		foodMapper.updateFoodTypeById(foodType);
	}

	@Override
	public void deleteFoodTypeById(Account account, Integer id) {
		FoodType foodType = new FoodType();
		foodType.setId(id);
		foodType.setOperatorId(account.getOperatorId());
		foodType.setStatus(2);
		foodMapper.deleteFoodTypeById(foodType);
	}
	
	@Override
	public boolean insertFood(HttpServletRequest request, Food food) {

		List<String> imagesPath = FileUploadUtils.tranferFile(request,
				Constant.FOOD_PATH + Constant.PICTURE + "/" +food.getCategoryId());
		
		StringBuilder sb = new StringBuilder();
		if (imagesPath != null && imagesPath.size() > 0) {
			for (int i = 0; i < imagesPath.size(); i++) {
				if (i < imagesPath.size() - 1) {
					sb.append(imagesPath.get(i) + ",");
				} else {
					sb.append(imagesPath.get(i));
				}
			}
		}
        
		if(sb.length() > 0){
			food.setPicture1(sb.toString().replace(Constant.FOOD_PATH + Constant.PICTURE, Constant.FOOD_PATH + Constant.PICTURE1));
			food.setPicture2(sb.toString().replace(Constant.FOOD_PATH + Constant.PICTURE, Constant.FOOD_PATH + Constant.PICTURE2));
		}
		
		String path = request.getSession().getServletContext().getRealPath(Constant.FILE_BASE_PATH+Constant.FOOD_PATH + Constant.PICTURE + "/" +food.getCategoryId());
		String path1 = request.getSession().getServletContext().getRealPath(Constant.FILE_BASE_PATH+Constant.FOOD_PATH + Constant.PICTURE1 + "/" +food.getCategoryId());
		String path2 = request.getSession().getServletContext().getRealPath(Constant.FILE_BASE_PATH+Constant.FOOD_PATH + Constant.PICTURE2 + "/" +food.getCategoryId());
	
		File file = new File(path);
		if (file.exists()) {
			File[] filelist = file.listFiles();
			for (File f : filelist) {
				ImageUtil.compressImage(path + "/" + f.getName(),
						path1 + "/" + f.getName(), 445, 187);
				ImageUtil.compressImage(path + "/" + f.getName(),
						path2 + "/" + f.getName(), 200, 200);
			}
		}
		
		int status = foodMapper.insertFood(food);
		if (status > 0) {
			return true;
		}
		return false;
	}

	@Override
	public QueryResult queryFood(QueryFoodCategory queryFoodCategory) {
		QueryResult result = new QueryResult();
		result.setPage(queryFoodCategory.getPage());
		result.setPageSize(queryFoodCategory.getPageSize());
		result.setTotalRecord(foodMapper.findFoodCount(queryFoodCategory));
		result.setData(foodMapper.findFoodList(queryFoodCategory));
		return result;
	}
	
	@Override
	public List<Food> findFoodByCategoryId(Integer id) {
		return foodMapper.findFoodByCategoryId(id);
	}
	
	@Override
	public List<Food> findFoodByUnitId(Integer id) {
		return foodMapper.findFoodByUnitId(id);
	}
	
	@Override
	public Food findFoodById(Integer id) {
		return foodMapper.findFoodById(id);
	}

	@Override
	public Food findFoodBySku(String sku, Integer companyId) {
		return foodMapper.findFoodBySku(sku, companyId);
	}

	@Override
	@SuppressWarnings("unchecked")
	public void updateFoodById(Account account, HashMap<String, Object> map) {
		
		CustomHashMap<String, Object> item = CustomHashMap.convert(map);
		Food food = new Food();
		food.setId(item.getInteger("id"));
		food.setName(item.getString("name"));
		food.setGoodsPrice(item.getDouble("goods_price"));
		food.setOriginalPrice(item.getDouble("original_price"));
		food.setDescription(item.getString("description"));
		food.setWeight(item.getInteger("weight"));
		food.setOperatorId(account.getOperatorId());
		foodMapper.updateFoodById(food);
	}

	@Override
	public void deleteFoodById(Account account, Integer id) {
		Food food = new Food();
		food.setId(id);
		food.setOperatorId(account.getOperatorId());
		food.setStatus(2);
		foodMapper.deleteFoodById(food);
	}

}
